﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using UniversityStudentManagemnetSystem.BLL;
using UniversityStudentManagemnetSystem.DAO.DAL;

namespace UniversityStudentManagemnetSystem.UI
{
    public partial class TeacherPage : System.Web.UI.Page
    {
        TeacherManager teacherManager = new TeacherManager();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/Account/Login.aspx", false);
            }
            if (IsPostBack != true)
            {
                PopulatedDepartmentDropDownList();
                PopulatedDesignationDropDownList();
            }
        }

        private void PopulatedDepartmentDropDownList()
        {
            DepartmentManager departmentManager = new DepartmentManager();
            DataTable departments = departmentManager.GetAllDepartments();
            departmentDropDownList.DataTextField = "Name";
            departmentDropDownList.DataValueField = "Id";
            departmentDropDownList.DataSource = departments;
            departmentDropDownList.DataBind();
        }

    private void PopulatedDesignationDropDownList()
    {
        DesignationManager designationManager = new DesignationManager();
        List<Designation> designations = designationManager.GetDesignations();
        designationDropDownList.DataTextField = "DesignationName";
        designationDropDownList.DataValueField = "DesignationId";
        designationDropDownList.DataSource = designations;
        designationDropDownList.DataBind();
    }

        protected void teacherSaveButton_Click(object sender, EventArgs e)
        {
            string name = nameTextBox.Text;
            string address = addressTextBox.Text;
            string email = emailTextBox.Text;
            string contactNumber = contactNumberTextBox.Text;
            string maximumCredit = assignCreditTextBox.Text;

            Teacher teacher = new Teacher(name, address, email, contactNumber, maximumCredit);
            teacher.Department.DeptId = Convert.ToInt32(departmentDropDownList.SelectedValue);
            teacher.Designation.DesignationId = Convert.ToInt32(designationDropDownList.SelectedValue);

            TeacherManager teacherManager = new TeacherManager();
            try
            {
                string mesg = teacherManager.Save(teacher);
                teacherSaveMessageLabel.ForeColor = Color.Green;
                teacherSaveMessageLabel.Text = mesg;
            }
            catch (Exception exception)
            {
                teacherSaveMessageLabel.ForeColor = Color.Red;
                teacherSaveMessageLabel.Text = exception.Message;
            }

            ClearAllTextBoxes();


           
        }

        private void ClearAllTextBoxes()
        {
            nameTextBox.Text = string.Empty;
            addressTextBox.Text = string.Empty;
            emailTextBox.Text = string.Empty;
            contactNumberTextBox.Text = string.Empty;
            assignCreditTextBox.Text = string.Empty;
        }
    }
}